package com.tomcat.staticfile;

import java.io.*;
import java.net.Socket;

/**
 * Created by Administrator on 2016/4/29.
 */
public class StaticServer {
    private PrintStream out;
    private InputStream in;
    private  Socket socket;
    private String webRoot;

    public StaticServer(Socket socket){
        this.socket = socket;
        try{
            in = socket.getInputStream();
            out =new PrintStream( socket.getOutputStream());
        }catch (IOException e){
            e.printStackTrace();
        }
//        webRoot = System.getProperty("user.dir") +"/web/";
        /*webRoot = Thread.currentThread().getContextClassLoader().getResource("") +"web";
        webRoot = webRoot.replace("/target/classes","");
        webRoot = webRoot.replace("file:/","");*/
        webRoot = getCurrentProjectPath() +"web";
    }

    public static String getCurrentProjectPath(){
        String path = Thread.currentThread().getContextClassLoader().getResource("").toString() ;
        path = path.replace("/target/classes","");
        path = path.replace("file:/","");
        return path;
    }

    public void excute( String filename){
        sendFile(filename);
    }



    public void  sendErrorMessage(int code,String msg){
        out.println("HTTP/1.0 "+code+" "+msg);
        out.println("content-type:text/html");
        out.println();
        out.println("<html>");
        out.println("<head>");
        out.println("<meta charset=\"UTF-8\">");
        out.println("</head>");
        out.println("<body>");
        out.println("errorCode:"+code);
        out.println("<br/>");
        out.println("错误信息："+msg);

        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
    public void sendFile(String filename){
        System.out.println("WEB_ROOT+filename::"+webRoot+filename);
        File file = new File(webRoot+filename);
        if(!file.exists()){
            sendErrorMessage(404,"访问的文件不存在");
            return;
        }
        try {
            InputStream in = new FileInputStream(file);
            byte content[] = new byte[(int) file.length()];
            try {
                in.read(content);
                out.println("HTTP/1.0 200 query");
                out.println("content-length:" + content.length);
                out.println();
                out.write(content);
                out.flush();
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    public static void main(String [] args){
        String webRoot = System.getProperty("user.dir");
        System.out.println(webRoot);

        System.out.println("1:" + Thread.currentThread().getContextClassLoader().getResource(""));
        System.out.println("2:" + StaticServer.class.getClassLoader().getResource(""));
        System.out.println("3:" + ClassLoader.getSystemResource(""));
        System.out.println("4:" + StaticServer.class.getResource(""));//IdcardClient.class文件所在路径
        System.out.println("5:" + StaticServer.class.getResource("/")); // Class包所在路径，得到的是URL对象，用url.getPath()获取绝对路径String
    }
}
